<?php

namespace app\admin\model;

use think\Db;
use think\Model;
use my\Security;
use app\admin\model\AdminModel;

class OrderCommon extends Model
{
    protected $map = array();
    public function __construct()
    {
        parent::__construct();
        $areaInfo = AdminModel::getRoleArea();
        if(!empty($areaInfo)){
            $this->map['a.start_region_id'] = ['in',implode(',',$areaInfo['city'])];
        }
    }

    //订单列表
    public function getListInfo($page, $limit, $data)
    {
        $where = self::searchWhere($data);
        $where['a.is_del'] = 2;
        //分页
        $start = ($page - 1) * $limit;
        $count = Db::name('order_common')->alias('a')->join('driver_base b','a.driver_id = b.driver_id','left')->where($where)->where(function ($query) use ($data) {
            $whiteList = Db::name('white_list')->order('id desc')->find();
            if (!empty($data['all_order']) && (!empty($whiteList['uid_str']) || !empty($whiteList['driver_str']))) {
                if ($data['all_order'] == 1) {            //普通订单
                    $query->where(['a.uid' => ['not in', $whiteList['uid_str']]]);
                    $query->where(['a.driver_id' => ['not in', $whiteList['driver_str']]]);
                } elseif ($data['all_order'] == 2) {      //测试订单
                    $query->where(['a.uid' => ['in', $whiteList['uid_str']]]);
                    $query->where(['a.driver_id' => ['in', $whiteList['driver_str']]]);
                }
            }
        })->count();
        //加盟商筛选
        if (!empty($data['franchisee_info']) && $data['franchisee_info'] != "0") {
            $did = Db::name('driver_base')->where('franchisee_id', '=', $data['franchisee_info'])->field('driver_id')->select();
            $dids = [];
            foreach ($did as $k => $v) {
                $dids[$k] = $v['driver_id'];
            }
            $whereFran['a.driver_id'] = ['in', $dids];
        } elseif ($data['franchisee_info'] == "0") {
            $did = Db::name('driver_base')->where('franchisee_id', 'null')->field('driver_id')->select();
            $dids = [];
            foreach ($did as $k => $v) {
                $dids[$k] = $v['driver_id'];
            }
            $whereFran['a.driver_id'] = ['in', $dids];
        }
        $list = Db::name('order_common')->alias('a')->join('driver_base b','a.driver_id = b.driver_id','left')->where($where)->where($whereFran)->where(function ($query) use ($data) {
            $whiteList = Db::name('white_list')->order('id desc')->find();
            if (!empty($data['all_order']) && (!empty($whiteList['uid_str']) || !empty($whiteList['driver_str']))) {
                if ($data['all_order'] == 1) {            //普通订单
                    $query->where(['a.uid' => ['not in', $whiteList['uid_str']]]);
                    $query->where(['a.driver_id' => ['not in', $whiteList['driver_str']]]);
                } elseif ($data['all_order'] == 2) {      //测试订单
                    $query->where(['a.uid' => ['in', $whiteList['uid_str']]]);
                    $query->where(['a.driver_id' => ['in', $whiteList['driver_str']]]);
                }
            }
        })->field('a.*,b.driver_phone as real_phone')->limit($start, $limit)->order('common_id desc')->select();
        $whiteList = Db::name('white_list')->order('id desc')->find();
        foreach ($list as $key => $value) {
            //测试账号名单
            if (!empty($whiteList['uid_str']) && in_array($value['uid'], explode(',', $whiteList['uid_str']))) {
                $list[$key]['user_type'] = 1;
            } else {
                $list[$key]['user_type'] = 0;
            }
            //查询司机真实手机号码
            if (!empty($value['real_phone'])) {
                $list[$key]['real_phone'] = Security::decrypt($value['real_phone']);
            } else {
                $list[$key]['real_phone'] = '-';
            }
            //类型
            if (!empty($whiteList['driver_str']) && in_array($value['driver_id'], explode(',', $whiteList['driver_str']))) {
                $list[$key]['driver_type'] = 1;
            } else {
                $list[$key]['driver_type'] = 0;
            }
            $list[$key]['order_sn'] = $value['abnormal_order'] == 1 ? "<span style='color: red;'>" . $value['order_sn'] . "</span>" : $value['order_sn'];
            $list[$key]['order_time'] = date("Y-m-d H:i:s", $value['order_time']);                                     //下单时间
            $list[$key]['on_car_time'] = !empty($value['on_car_time']) ? date("Y-m-d H:i:s", $value['on_car_time']) : '-';  //上车时间
            //查询异常订单是否存在订单金额冻结记录
            $list[$key]['abnormal_status'] = 0;
            if ($value['abnormal_order'] == 1) {
                $driverWalletInfo = Db::name('driver_wallet_record')->where(['driver_id' => $value['driver_id'], 'common_id' => $value['common_id'], 'record_type' => 5])->find();
                if (!empty($driverWalletInfo)) {
                    $list[$key]['abnormal_status'] = 1;
                }
            }
            //查询属于的角色组
            $role_res = Db::table('xm_admin')->field('role_id')->where(['admin_id' => session('admin_id')])->find();
            if ($role_res['role_id'] == '27') {
                $list[$key]['passenger_phone'] = substr_replace(Security::decrypt($value['passenger_phone']), '****', 3, 4);                                      //乘客手机号
            } else {
                $list[$key]['passenger_phone'] = Security::decrypt($value['passenger_phone']);                                         //乘客手机号
            }
            $list[$key]['driver_phone'] = !empty($value['driver_phone']) ? Security::decrypt($value['driver_phone']) : '-';         //司机手机号
            $list[$key]['start_name'] = Security::decrypt($value['start_name']);                                              //出发地点
            $list[$key]['arrive_name'] = Security::decrypt($value['arrive_name']);                                             //目的地
            $list[$key]['original_price'] = sprintf("%.2f", $value['original_price'] / 100);                       //原价
            $list[$key]['orderStatus'] = order_status($value['order_status']);                                                 //订单状态
            $list[$key]['freeze_status'] = freeze_status($value['freeze_status']);                                                 //冻结状态
            $list[$key]['orderTypeName'] = $value['book'] == 1 ? '预约订单' : '实时订单';
            $franchisee_id = Db::table('xm_driver_base')->where('driver_id', $value['driver_id'])->value('franchisee_id');
            $db2 = Db::connect('db2');
            $list[$key]['franchisee_name'] = $db2->table('franchisee')->where('id', $franchisee_id)->value('name');
            //查询出发省份 - 城市
            if (!empty($value['start_region_id'])) {
                $regionInfo = Db::name('region')->where(['region_id' => $value['start_region_id']])->find();
                if (!empty($regionInfo)) {
                    if ($regionInfo['region_level'] == 3) {       //区域
                        $cityInfo = Db::name('region')->where(['region_id' => $regionInfo['parent_id']])->find();
                        $provinceInfo = Db::name('region')->where(['region_id' => $cityInfo['parent_id']])->find();
                        $list[$key]['dressName'] = $provinceInfo['region_name'] . '/' . $cityInfo['region_name'];   //省份 + //城市
                    } elseif ($regionInfo['region_level'] == 2) {  //城市
                        $provinceInfo = Db::name('region')->where(['region_id' => $regionInfo['parent_id']])->find();
                        $list[$key]['dressName'] = $provinceInfo['region_name'] . '/' . $regionInfo['region_name'];   //省份 + //城市
                    }
                } else {
                    $list[$key]['dressName'] = '-';
                }
            }
        }
        return array(
            'code' => 0,
            'msg' => '',
            'count' => $count,
            'data' => $list
        );
    }

    //等待详细和数据导出
    public function getOrderInfo($data)
    {
        $where = self::searchWhere($data);
        $where['a.is_del'] = 2;
        $list = Db::name('order_common a')->join('driver_base b','a.driver_id = b.driver_id','left')->join(['xm_comment' => 'c'], 'a.common_id = c.common_id', 'LEFT')
            ->where($where)->where(function ($query) use ($data) {
                $whiteList = Db::name('white_list')->order('id desc')->find();
                if (!empty($data['all_order']) && (!empty($whiteList['uid_str']) || !empty($whiteList['driver_str']))) {
                    if ($data['all_order'] == 1) {            //普通订单
                        $query->whereOr(['a.uid' => ['not in', $whiteList['uid_str']]]);
                        $query->whereOr(['a.driver_id' => ['not in', $whiteList['driver_str']]]);
                    } elseif ($data['all_order'] == 2) {      //测试订单
                        $query->whereOr(['a.uid' => ['in', $whiteList['uid_str']]]);
                        $query->whereOr(['a.driver_id' => ['in', $whiteList['driver_str']]]);
                    }
                }
            })->field('a.*,b.driver_phone as real_phone,c.content,c.time,c.service_rank,comment_dict_id,c.label_id')->order('a.common_id desc')->select();
        $result = [];
        foreach ($list as $key => $value) {
            //订单ID
            $result[$key]['common_id'] = $value['common_id'];
            $result[$key]['abnormal_order'] = $value['abnormal_order'];

            //查询属于的角色组
            $role_res = Db::table('xm_admin')->field('role_id')->where(['admin_id' => session('admin_id')])->find();
            if ($role_res['role_id'] == '27') {
                $result[$key]['abnormalName'] = '-';
                $result[$key]['abnormal_reason'] = '-';
            } else {
                if (!empty($value['abnormal_order'])) {
                    $result[$key]['abnormalName'] = '异常订单';
                    $result[$key]['abnormal_reason'] = $value['abnormal_reason'];
                } else {
                    $result[$key]['abnormalName'] = '-';
                    $result[$key]['abnormal_reason'] = '-';
                }
            }
            //查询出发省份 - 城市
            if (!empty($value['start_region_id'])) {
                $regionInfo = Db::name('region')->where(['region_id' => $value['start_region_id']])->find();
                if (!empty($regionInfo)) {
                    if ($regionInfo['region_level'] == 3) {       //区域
                        $cityInfo = Db::name('region')->where(['region_id' => $regionInfo['parent_id']])->find();      //城市
                        $provinceInfo = Db::name('region')->where(['region_id' => $cityInfo['parent_id']])->find();        //省份
                        $result[$key]['province'] = $provinceInfo['region_name'];
                        $result[$key]['city'] = $cityInfo['region_name'];
                    } elseif ($regionInfo['region_level'] == 2) {  //城市
                        $provinceInfo = Db::name('region')->where(['region_id' => $regionInfo['parent_id']])->find();      //省份
                        $result[$key]['province'] = $provinceInfo['region_name'];
                        $result[$key]['city'] = $regionInfo['region_name'];
                    }
                } else {
                    $result[$key]['province'] = '-';
                    $result[$key]['city'] = '-';
                }
            }
            $result[$key]['real_phone'] = $value['real_phone']?Security::decrypt($value['real_phone']):'-';
            $result[$key]['book'] = $value['book'];
            $result[$key]['book_time'] = !empty($value['book_time']) ? date("Y-m-d H:i:s", $value['book_time']) : '-';                              //预约出发时间
            $result[$key]['order_sn'] = $value['order_sn'];                                                                                                 //订单号
            $result[$key]['bookType'] = $value['book'] == 1 ? "预约订单" : "实时订单";                                                                      //订单类型

            //查询属于的角色组
            $role_res = Db::table('xm_admin')->field('role_id')->where(['admin_id' => session('admin_id')])->find();
            if ($role_res['role_id'] == '27') {
                $result[$key]['passenger_phone'] = substr_replace(Security::decrypt($value['passenger_phone']), '****', 3, 4); //乘客手机号
            } else {
                $result[$key]['passenger_phone'] = Security::decrypt($value['passenger_phone']);                                                            //乘客手机号
            }
//            $result[$key]['passenger_phone']    = Security::decrypt($value['passenger_phone']);                                                           //乘客手机号
            $result[$key]['order_time'] = !empty($value['order_time']) ? date("Y-m-d H:i:s", $value['order_time']) : '-';                           //下单时间
            $result[$key]['start_name'] = Security::decrypt($value['start_name']);                                                                          //出发地点
            $result[$key]['arrive_name'] = Security::decrypt($value['arrive_name']);                                                                        //目的地
            $result[$key]['start_time'] = !empty($value['start_time']) ? date("Y-m-d H:i:s", $value['start_time']) : '-';                           //下单时间
            $result[$key]['book_time'] = !empty($value['book_time']) ? date("Y-m-d H:i:s", $value['book_time']) : '-';                              //预约订单出发时间
            $result[$key]['depart_time'] = !empty($value['depart_time']) ? date("Y-m-d H:i:s", $value['depart_time']) : '';                         //预约订单 - 司机出发时间
            $result[$key]['arrive_appointment_time'] = !empty($value['arrive_appointment_time']) ? date("Y-m-d H:i:s", $value['arrive_appointment_time']) : '';//预约订单 - 司机到达约定地点时间
            $result[$key]['order_taker_time'] = !empty($value['order_taker_time']) ? date("Y-m-d H:i:s", $value['order_taker_time']) : '-';         //司机抢单-出发时间
            $result[$key]['on_car_time'] = !empty($value['on_car_time']) ? date("Y-m-d H:i:s", $value['on_car_time']) : '-';                        //上车时间
            $result[$key]['off_car_time'] = !empty($value['off_car_time']) ? date("Y-m-d H:i:s", $value['off_car_time']) : '-';                     //下车时间
            $result[$key]['driver_name'] = $value['driver_name'];                                                                                           //司机姓名
            $result[$key]['driver_phone'] = !empty($value['driver_phone']) ? Security::decrypt($value['driver_phone']) : '-';                               //司机电话;
            $result[$key]['orderStatus'] = $value['order_status'];
            $result[$key]['order_status'] = order_status($value['order_status']);                                                                           //订单状态
            $result[$key]['is_pay'] = $value['order_status'] >= 45 ? "已付款" : "未付款";                                                                 //付款状态
            $result[$key]['is_evalute'] = $value['order_status'] >= 50 ? "已评价" : "未评价";                                                             //评价状态
            $result[$key]['original_price'] = sprintf("%.2f", $value['original_price'] / 100);                                              //订单金额
            $result[$key]['driverMoney'] = sprintf("%.2f", ($value['original_price'] - $value['dispatch_fee']) / 100);                      //实际车费
            $result[$key]['dispatch_fee'] = sprintf("%.2f", $value['dispatch_fee'] / 100);                                                  //调度费
            $result[$key]['coupon_price'] = sprintf("%.2f", $value['coupon_price'] / 100);                                                  //优惠金额
            $result[$key]['need_price'] = sprintf("%.2f", $value['need_price'] / 100);                                                      //实付金额
            if($value['coupon_id'] && $value['coupon_type'] == 2){
                $result[$key]['coupon_info'] = '海宝商品通用券';
            }elseif($value['coupon_id'] && $value['coupon_type'] == 1){
                $result[$key]['coupon_info'] = '小马出行券';
            }else{
                $result[$key]['coupon_info'] = '-';
            }
            //查询支付时间 - 先判断支付是否存在 - 不存在去流水记录查询
            if (!empty($value['pay_time'])) {
                $result[$key]['pay_time'] = !empty($value['pay_time']) ? date("Y-m-d H:i:s", $value['pay_time']) : '';   //支付时间
            } else {
                $time = Db::name('user_recharge')->where(['out_trade_no' => $value['order_sn']])->value('time');
                $result[$key]['pay_time'] = !empty($time) ? date("Y-m-d H:i:s", $time) : '';                              //支付时间
            }
            $result[$key]['platform_price'] = sprintf("%.2f", $value['platform_price'] / 100);                   //平台费
            //查询司机收入 支付成功 - 司机已上车
            if ($value['order_status'] >= 45) {
                $driverIncome = Db::table('xm_driver_wallet_record')->where(['record_type' => 1, 'common_id' => $value['common_id']])->value('money');
                $result[$key]['driver_income'] = sprintf("%.2f", $driverIncome / 100);                             //订单司机收入
                //查询抵扣金额
                $gifts_money  = Db::name('user_recharge')->where(['out_trade_no' => $value['order_sn'], 'money_statement' => 2])->value('gifts_money');
                $result[$key]['gifts_money']   = sprintf("%.2f", $gifts_money / 100);
            } else {
                $result[$key]['driver_income'] = '-';
                $result[$key]['gifts_money']   = '-';
            }
            //if ($value['freeze_status']==1){    //暂时不做判断
            $freeze = Db::table('xm_driver_wallet_record')->where(['record_type' => 5, 'common_id' => $value['common_id']])->value('money');
            $result[$key]['freeze_income'] = sprintf("%.2f", $freeze / 100);
            //}else{
            //  $result[$key]['freeze_income'] = '-';
            //}
            $result[$key]['payTypeName'] = !empty($value['pay_type']) ? pay_type($value['pay_type']) : '-';                     //支付方式
            $result[$key]['information'] = $value['tardy_time_reason'];                                                     //迟到信息
            $result[$key]['driver_fine'] = sprintf("%.2f", $value['driver_fine'] / 100);                      //司机罚款
            $result[$key]['user_price'] = sprintf("%.2f", $value['passenger_cancel_fee'] / 100);             //乘客取消费用
            //查询乘客有责取消的数据信息
            $userRechargeInfo = [];
            if ($value['order_status'] == 12) {
                $userRechargeInfo = Db::name('user_recharge')->where(['uid' => $value['uid'], 'out_trade_no' => $value['order_sn'], 'money_statement' => ['in', [4, 5]]])->field('type,time')->find();
                if (!empty($userRechargeInfo)) {
                    $result[$key]['payStatus'] = 1;
                    $result[$key]['payStatusInfo'] = '已支付';
                    $result[$key]['payTimeInfo'] = date("Y-m-d H:i:s", $userRechargeInfo['time']);
                    $result[$key]['payTypeInfo'] = pay_type($userRechargeInfo['type']);
                } else {
                    $result[$key]['payStatus'] = 0;
                    $result[$key]['payStatusInfo'] = '待支付';
                    $result[$key]['payTimeInfo'] = '-';
                    $result[$key]['payTypeInfo'] = '-';
                }
            } else {
                $result[$key]['payStatus'] = '-';
                $result[$key]['payStatusInfo'] = '-';
                $result[$key]['payTimeInfo'] = '-';
                $result[$key]['payTypeInfo'] = '-';
            }
            $result[$key]['maybe_distance'] = $value['maybe_distance'] / 1000;                                                  //预估里程
            $result[$key]['maybe_fee'] = sprintf("%.2f", $value['maybe_fee'] / 100);                      //预估费用
            $result[$key]['real_fee'] = sprintf("%.2f", $value['real_fee'] / 100);                       //实际费用
            $result[$key]['real_distance'] = $value['real_distance'] / 1000;                                                   //实际里程
            //评价信息
            $result[$key]['score'] = $value['service_rank'];                                                         //评分
            //取消订单
            $result[$key]['cancel_time'] = !empty($value['cancel_time']) ? date("Y-m-d H:i:s", $value['cancel_time']) : '-';                  //取消时间
            $result[$key]['cancel_content'] = $value['cancel_content'];                                                        //取消原因
            //司机车辆信息
            $carInfo = [];
            if (!empty($value['car_id'])) {
                $carInfo = Db::name('car_info')->where(['car_id' => $value['car_id']])->field('car_plate,car_brand,color')->find();
                $result[$key]['car_plate'] = !empty($carInfo['car_plate']) ? Security::decrypt($carInfo['car_plate']) : '';
                $result[$key]['car_info'] = Security::decrypt($carInfo['car_brand']) . ' - ' . Security::decrypt($carInfo['color']);
            } else {
                $result[$key]['car_plate'] = '';
                $result[$key]['car_info'] = '';
            }
            $label_content = '';
            if (!empty($value['comment_dict_id'])) {
                $label_content = Db::name('comment_dict')->where(['id' => $value['comment_dict_id']])->value('title');
            }

            if ($value['label_id']) {
                $label_id = rtrim($value['label_id'], ',');
                $label_ids = explode(',', $label_id);
                foreach ($label_ids as $val) {
                    $label_res = Db::table('xm_comment_label_dict')->field('label')->where(['status' => 1, 'id' => $val])->find();
                    $label_content .= '[' . $label_res['label'] . ']';
                }
            }
            $result[$key]['evaluate'] = $label_content . $value['content'];                                     //评价内容
            $result[$key]['evaluate_time'] = !empty($value['time']) ? date("Y-m-d H:i:s", $value['time']) : '-';//评价时间
            //汽车ID
            $result[$key]['car_id'] = $value['car_id'];                                                                     //汽车ID
        }
        return $result;
    }

    //检索条件处理
    public function searchWhere($data = [])
    {
        $where = $this->map;
        //订单ID检索
        if (!empty($data['common_id'])) {
            $where['a.common_id'] = $data['common_id'];
        }
        //城市搜索
        if (!empty($data['province_id']) && !empty($data['city_id'])) {
            $where['a.start_region_id'] = $data['city_id'];
        } else {
            //省份搜索
            if (!empty($data['province_id'])) {
                //查询该省份所以城市ID
                $cityList = Db::name('region')->where('parent_id', $data['province_id'])->field('region_id')->select();
                if (!empty($cityList)) {
                    $city_id = '';
                    foreach ($cityList as $value) {
                        $city_id .= $value['region_id'] . ',';
                    }
                    $city_id = rtrim($city_id, ',');
                    $areaList = Db::name('region')->where(['parent_id' => ['in', rtrim($city_id, ',')]])->field('region_id')->select();
                    foreach ($areaList as $value) {
                        $city_id .= $value['region_id'] . ',';
                    }
                    $where['a.start_region_id'] = ['in', rtrim($city_id, ',')];
                }
            }
        }
        //订单类型
        if (!empty($data['book'])) {
            $where['a.book'] = $data['book'];
        }
        //下单时间
        if (!empty($data['start_time'])) {
            $where['a.order_time'] = ['>=', strtotime($data['start_time'])];
        }
        if (!empty($data['end_time'])) {
            $where['a.order_time'] = ['<=', strtotime($data['end_time'] . " 23:59:59")];
        }
        if (!empty($data['start_time']) && !empty($data['end_time'])) {
            $where['a.order_time'] = ['between', [strtotime($data['start_time']), strtotime($data['end_time'] . " 23:59:59")]];
        }
        //订单号
        if (!empty($data['order_sn'])) {
            $where['a.order_sn'] = ['like', "%" . $data['order_sn'] . "%"];
        }
        //乘客手机号
        if (!empty($data['passenger_phone'])) {
            $where['a.passenger_phone'] = Security::encrypt($data['passenger_phone']);
        }
        //司机手机号
        if (!empty($data['driver_phone'])) {
            $where['a.driver_phone'] = Security::encrypt($data['driver_phone']);
        }
        //司机注册手机号
        if (!empty($data['real_phone'])) {
            $where['b.driver_phone'] = Security::encrypt($data['real_phone']);
        }
        //订单状态
        if ($data['order_status'] != '') {
            if ($data['order_status'] == 45) {
                $where['a.order_status'] = ['>=', $data['order_status']];
            } else {
                $where['a.order_status'] = $data['order_status'];
            }
        }
        //异常订单
        if (!empty($data['abnormal_order'])) {
            //2020.7.15 lili 无异常订单
            if($data['abnormal_order']==-1){
                $where['a.abnormal_order'] =['<', 1];
            }else{
                $where['a.abnormal_order'] = $data['abnormal_order'];
            }
        }
        //冻结金额
        if (!empty($data['freeze_status'])) {
            $where['a.freeze_status'] = $data['freeze_status'];
        }
        //支付方式
        if (!empty($data['pay_type'])) {
            $where['a.pay_type'] = $data['pay_type'];
        }
        //支付时间 - 开始
        if (!empty($data['start_pay_time']) && empty($data['end_pay_time'])) {
            $where['a.pay_time'] = ['>=', strtotime($data['start_pay_time'])];
        }
        //支付时间 - 结束
        if (empty($data['start_pay_time']) && !empty($data['end_pay_time'])) {
            $where['a.pay_time'] = ['>=', strtotime($data['end_pay_time'] . " 23:59:59")];
        }
        //支付时间 - 开始/结束
        if (!empty($data['start_pay_time']) && !empty($data['end_pay_time'])) {
            $where['a.pay_time'] = ['between', [strtotime($data['start_pay_time']), strtotime($data['end_pay_time'] . " 23:59:59")]];
        }
        //加盟商信息
        if (session('admin_type') == 5 && session('franchisee_str') != '') {
            //$where['a.franchisee_id'] = ['in',session('franchisee_str')];
            //查询加盟商明下有多少司机
            $driverList = Db::name('driver_base')->where(['franchisee_id' => ['in', session('franchisee_str')]])->field('driver_id')->select();
            $driverStr = '';
            foreach ($driverList as $key => $value) {
                $driverStr .= $value['driver_id'] . ',';
            }
            $where['a.driver_id'] = ['in', rtrim($driverStr, ',')];
        }
        return $where;
    }

    /*------------------专快车订单数据统计功能如下-------------------*/
    public function totalInfo($dateArray = [], $all_order = '', $disposition = [])
    {
        $where = $this->map;
        //查询设置 - 测试账号信息
        $whiteList = Db::name('white_list')->order('id desc')->find();
        if (!empty($all_order) && (!empty($whiteList['uid_str']) || !empty($whiteList['driver_str']))) {
            if ($all_order == 1) {
                $where['uid'] = ['not in', $whiteList['uid_str']];
                $where['driver_id'] = ['not in', $whiteList['driver_str']];      //普通订单
            } elseif ($all_order == 2) {
                $where['uid'] = ['in', $whiteList['uid_str']];
                $where['driver_id'] = ['in', $whiteList['driver_str']];          //测试订单
            }
        }
        if (!empty($disposition[0]) && empty($disposition[1])) {
            $ids = Db::name('region')->where('parent_id', $disposition[0])->field('region_id')->select();
            $temp=[];
            foreach ($ids as $k=>$v){
                $temp[$k]=$v['region_id'];
            }
            $where['a.start_region_id']=['in',$temp];
        }elseif(!empty($disposition[0]) && !empty($disposition[1])){
            $where['a.start_region_id'] = $disposition[1];
        }
        //循环查询订单数据
        foreach ($dateArray as $key => $value) {
            $start_time = strtotime($value);
            $end_time = strtotime($value . ' 23:59:59');
            if (!empty($start_time) && !empty($end_time)) {
                $where['order_time'] = ['between', [$start_time, $end_time]];
            }
            $where['is_del'] = 2;
            //订单总数
            $orderInfo[$value]['count'] = Db::name('order_common a')->where($where)->count();
            //时时订单
            $orderInfo[$value]['time_count'] = Db::name('order_common a')->where($where)->where(['book' => 2])->count();
            //预约订单
            $orderInfo[$value]['make_count'] = Db::name('order_common a')->where($where)->where(['book' => 1])->count();
            //取消订单数量
            $orderInfo[$value]['cancel_count'] = Db::name('order_common a')->where($where)->where(['order_status' => ['in', [5, 10, 12, 15]]])->count();
            //待接单数量
            $orderInfo[$value]['wait_count'] = Db::name('order_common a')->where($where)->where(['order_status' => ['in', [0]]])->count();
            //已接单数量
            $orderInfo[$value]['take_count'] = Db::name('order_common a')->where($where)->where(['order_status' => ['between', [16, 30]]])->count();
            //已完成数量
            $orderInfo[$value]['finish_count'] = Db::name('order_common a')->where($where)->where(['order_status' => ['>=', 40]])->count();
            //查询已完成订单数量金额统计
            $finishList = Db::name('order_common a')->where($where)->where(['order_status' => ['>=', 45]])->select();
            if (!empty($finishList)) {
                $need_price = $coupon_price = '0';
                foreach ($finishList as $kly => $val) {
                    $need_price += $val['need_price'];
                    $coupon_price += $val['coupon_price'];
                }
                $orderInfo[$value]['need_price'] = sprintf("%.2f", $need_price / 100);
                $orderInfo[$value]['coupon_price'] = sprintf("%.2f", $coupon_price / 100);
            } else {
                $orderInfo[$value]['need_price'] = 0;
                $orderInfo[$value]['coupon_price'] = 0;
            }
        }
        $result = [];
        foreach ($orderInfo as $key => $value) {
            $result['count'] .= $value['count'] . ',';
            $result['time_count'] .= $value['time_count'] . ',';
            $result['make_count'] .= $value['make_count'] . ',';
            $result['cancel_count'] .= $value['cancel_count'] . ',';
            $result['wait_count'] .= $value['wait_count'] . ',';
            $result['take_count'] .= $value['take_count'] . ',';
            $result['finish_count'] .= $value['finish_count'] . ',';
            $result['need_price'] .= $value['need_price'] . ',';
            $result['coupon_price'] .= $value['coupon_price'] . ',';
        }
        return $result;
    }

    //查询高峰期订单数量
    public function timeOrderNum($dateArray = [], $all_order = '',$disposition=[])
    {
        if ($disposition[0]) {
            $ids = Db::name('region')->where('parent_id', $disposition[0])->field('region_id')->select();
            $temp=[];
            foreach ($ids as $k=>$v){
                $temp[$k]=$v['region_id'];
            }
            $where['start_region_id']=['in',$temp];
        }
        if ($disposition[1]) {
            $id=Db::name('region')->where('region_id', $disposition[1])->value('region_id');
            $where['start_region_id'] = ['=',$id];
        }
        //查询设置 - 测试账号信息
        $whiteList = Db::name('white_list')->order('id desc')->find();
        if (!empty($all_order) && (!empty($whiteList['uid_str']) || !empty($whiteList['driver_str']))) {
            if ($all_order == 1) {
                $where['uid'] = ['not in', $whiteList['uid_str']];
                $where['driver_id'] = ['not in', $whiteList['driver_str']];      //普通订单
            } elseif ($all_order == 2) {
                $where['uid'] = ['in', $whiteList['uid_str']];
                $where['driver_id'] = ['in', $whiteList['driver_str']];          //测试订单
            }
        }
        for ($i = 0; $i <= 23; $i++) {
            foreach ($dateArray as $key => $value) {
                if (strlen($i) == 1) {
                    $i = '0' . $i;
                }
                $start_time = $value . " " . $i . ":00:00";
                $end_time = $value . " " . $i . ":59:59";
                $where['order_time'] = ['between', [strtotime($start_time), strtotime($end_time)]];
                $where['is_del'] = 2;
                $orderInfo[$i][] = Db::name('order_common')->where($where)->count();
            }
        }
        foreach ($orderInfo as $key => $value) {
            $sumOrderNum = 0;
            foreach ($value as $val) {
                $sumOrderNum += $val;
            }
            $arr[] = $sumOrderNum;
        }
        $sumOrderNumStr = implode(',', $arr);
        return $sumOrderNumStr;
    }
}
